
/**   
 * @Title: LogUtil.java 
 * @Package: com.rename.icode.utils 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午4:36:07 
 * @version  
 */


package com.rename.icode.utils;

/** 
 * @Description 自动打印日志所在类和方法名，所在行等详细信息。输出格式：类名.方法名():行数:输出信息。
 * @author Administrator
 * @date 2015-2-11 下午4:36:07 
 * @version V1.0
 */

public class LogUtil {
	private static final String TAG = "LogUtil";

	public static final int NODEBUG = 0;// 不打印log日志
	public static final int VERBOSE = 1;
	public static final int DEBUG = 2;
	public static final int INFO = 3;
	public static final int WARN = 4;
	public static final int ERROR = 5;

	private static int logLevel = LogUtil.ERROR;

	public LogUtil() {

	}

	/**
	 * Change current logging level
	 * 
	 * @param level
	 *            new log level 1 <= level <= 6
	 */
	public static void setLogLevel(int level) {
		logLevel = level;
	}

	/**
	 * Get the current log level
	 * 
	 * @return the log level
	 */
	public static int getLogLevel() {
		return logLevel;
	}

	/**
	 * Send a VERBOSE log message.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void v(String msg) {
		if (logLevel >= LogUtil.VERBOSE) {
			android.util.Log.v(TAG, buildMessage(msg));
		}
	}

	/**
	 * Send a VERBOSE log message.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void v(String tag, String msg) {
		if (logLevel >= LogUtil.VERBOSE) {
			android.util.Log.v(tag, buildMessage(msg));
		}
	}

	/**
	 * Send a DEBUG log message.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void d(String msg) {
		if (logLevel >= LogUtil.DEBUG) {
			android.util.Log.d(TAG, buildMessage(msg));
		}
	}

	/**
	 * Send a DEBUG log message.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void d(String tag, String msg) {
		if (logLevel >= LogUtil.DEBUG) {
			android.util.Log.d(tag, buildMessage(msg));
		}
	}

	/**
	 * Send an INFO log message.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void i(String msg) {
		if (logLevel >= LogUtil.INFO) {
			android.util.Log.i(TAG, buildMessage(msg));
		}

	}

	/**
	 * Send an INFO log message.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void i(String tag, String msg) {
		if (logLevel >= LogUtil.INFO) {
			android.util.Log.i(tag, buildMessage(msg));
		}

	}

	/**
	 * Send a WARN log message
	 * 
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void w(String msg) {
		if (logLevel >= LogUtil.WARN) {
			android.util.Log.w(TAG, buildMessage(msg));
		}
	}

	/**
	 * Send a WARN log message
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void w(String tag, String msg) {
		if (logLevel >= LogUtil.WARN) {
			android.util.Log.w(tag, buildMessage(msg));
		}
	}

	/**
	 * Send a WARN log message and log the exception.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 * @param thr
	 *            An exception to log
	 */
	public static void w(String msg, Throwable thr) {
		if (logLevel >= LogUtil.WARN) {
			android.util.Log.w(TAG, buildMessage(msg), thr);
		}
	}

	/**
	 * Send a WARN log message and log the exception.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 * @param thr
	 *            An exception to log
	 */
	public static void w(String tag, String msg, Throwable thr) {
		if (logLevel >= LogUtil.WARN) {
			android.util.Log.w(tag, buildMessage(msg), thr);
		}
	}

	/**
	 * Send an ERROR log message.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void e(String msg) {
		if (logLevel >= LogUtil.ERROR) {
			android.util.Log.e(TAG, buildMessage(msg));
		}
	}

	/**
	 * Send an ERROR log message.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 */
	public static void e(String tag, String msg) {
		if (logLevel >= LogUtil.ERROR) {
			android.util.Log.e(tag, buildMessage(msg));
		}
	}

	/**
	 * Send an ERROR log message and log the exception.
	 * 
	 * @param msg
	 *            The message you would like logged.
	 * @param thr
	 *            An exception to log
	 */
	public static void e(String msg, Throwable thr) {
		if (logLevel >= LogUtil.ERROR) {
			android.util.Log.e(TAG, buildMessage(msg), thr);
		}
	}

	/**
	 * Send an ERROR log message and log the exception.
	 * 
	 * @param tag
	 *            the log tag
	 * @param msg
	 *            The message you would like logged.
	 * @param thr
	 *            An exception to log
	 */
	public static void e(String tag, String msg, Throwable thr) {
		if (logLevel >= LogUtil.ERROR) {
			android.util.Log.e(tag, buildMessage(msg), thr);
		}
	}

	/**
	 * Building Message
	 * 
	 * @param msg
	 *            The message you would like logged.
	 * @return Message String
	 */
	protected static String buildMessage(String msg) {

		StackTraceElement caller = new Throwable().fillInStackTrace()
				.getStackTrace()[2];

		return new StringBuilder().append(caller.getClassName()).append(".")
				.append(caller.getMethodName()).append("():")
				.append(caller.getLineNumber()).append(":").append(msg)
				.toString();
	}
}
